<?php
/**
 * Created by PhpStorm.
 * User: luoqinghui
 * Date: 2020/7/24
 * Time: 上午17:40
 */

namespace App\Http\Controllers\Api\Task;

use App\Http\Controllers\Controller;
use App\Modules\Task\Services\TaskService;
use App\Modules\Task\Services\DrawTaskService;
use Illuminate\Http\Request;

/**
 * 任务模块
 * Class TaskController
 * @package App\Http\Controllers\Api\Task
 */
class TaskController extends Controller
{
    /**
     * 任务列表
     */
    public function getList(Request $request){
        $size = $request->input('size',15);
        $result = TaskService::getList(['size'=>$size]);
        //如果未登录 直接返回任务列表
        if ( empty($result) || $this->isNotLogin() ) {
            return response()->pagination($result);
        }
//        if ( empty($result) ) {
//            return response()->pagination($result);
//        }
        //用户信息
        $userData = $this->getLoginUser();
        $userData = [
            'id'            => $userData->id,
            'target_value'  => $userData->target_value,
        ];
        $data = [];
        //用户是否领取判断
        $result = $result->toArray();
        $taskObj = new TaskService();
        $data['list'] = $taskObj->taskTrait($userData,$result['data']);
        $data['target_value'] = $userData['target_value'];
        $data['meta']= [
            'total'=>(int)$result['total'],
            'per_page'=>(int)$result['per_page'],
            'current_page'=>(int)$result['current_page'],
            'last_page'=>(int)$result['last_page'],
            'next_page_url'=>(string)$result['next_page_url'],
            'prev_page_url'=>(string)$result['prev_page_url']
        ];
        return response()->success($data);
    }

    /**
     * 获取任务详情
     */
    public function getTask(Request $request){
        $task_id = $request->input('id',0);
        if ( empty($task_id) ) return response()->error("PARAM_EMPTY");
        return response()->success(TaskService::getTask($task_id,'id'));
    }

    /**
     * 领取任务
     */
    public function addDrawTask(Request $request){
        //数据验证
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $task_id =$request->input('task_id',0);
        if ( empty($user_id) || empty($task_id) ) return response()->error("PARAM_EMPTY");
        //查出当前任务
        $taskData = TaskService::getTask($task_id,'id');
        if ( empty($taskData) ) return response()->error("PARAM_ERROR");
        //判断当天是否有领取任务
        $drawTaskData = DrawTaskService::getDrawTask($taskData->code,$user_id);
        if ( !empty($drawTaskData) ) return response()->error("REPEAT_RECEIVE");
        $start_time = strtotime($taskData->start_time);
        $end_time = strtotime($taskData->end_time);
        //判断是否在任务时间的内领取任务
        $hours = strtotime(date('H:i',time()));
        if ( $hours < $start_time || $hours >= $end_time ) return response()->error("NOT_PERIOD_TIME");
        //添加任务数据
        $dataAddArr = [
            'user_id'   => $user_id,
            'task_id'   => $task_id,
            'task_code' => $taskData->code,
            'coin'      => $taskData->coin,
        ];
        $result = DrawTaskService::addDrawTask($dataAddArr);
        if ( empty($result) ) return response()->error("ADD_FAILURE");
        return response()->success();
    }

    /**
     * 任务头部
     */
    public function glucoseRate(){
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        return response()->success(TaskService::glucoseRate($user_id));
    }
}
























